package su.msk.jet.team01.chatik.server.commands;

import javax.naming.InvalidNameException;

import su.msk.jet.team01.chatik.server.data.Client;
import su.msk.jet.team01.chatik.server.exception.InvalidArgumentException;
import su.msk.jet.team01.chatik.server.exception.NameInUseException;

public class ChangeNameCommand extends Command {
	private static final String CLIENT_INFO_MSG_INVALID_USER_NAME_LENGHT = "Unable to change name. Maximum name length is %d.";
	private static final String CLIENT_INFO_MSG_NAME_CHANGED = "Name successfully changed to %s.";
	private static final int MAX_USER_NAME_LENGTH = 100;

	public ChangeNameCommand() {
		super("name", 1);
	}

	@Override
	public void doExecute(final Client client, final String[] args)
			throws InvalidArgumentException {
		String name = args[0];

		validateHaveNoSpaces(name);
		try {
			validateLength(name, MAX_USER_NAME_LENGTH);
			client.changeName(name);
			client.send(CLIENT_INFO_MSG_NAME_CHANGED, name);
		} catch (InvalidArgumentException ex) {
			client.send(CLIENT_INFO_MSG_INVALID_USER_NAME_LENGHT,
					MAX_USER_NAME_LENGTH);
		} catch (InvalidNameException ex) {
			client.send(ex.getMessage());
		} catch (NameInUseException ex) {
			client.send(ex.getMessage());
		}
	}

}
